package Data;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.LineBorder;








public class DraggComponent extends JPanel implements ActionListener, MouseListener{

	
	private volatile int screenX = 200;
	private volatile int screenY = 200;
	private volatile int myX = 0;
	private volatile int myY = 0;
	private JPopupMenu menu;
	private JMenuItem makeInitial, makeFinal, makeNormal;

	public DraggComponent(String stateGraphicName) {
		setBorder(new LineBorder(Color.BLUE, 3));
		setBackground(Color.WHITE);
		setBounds(0, 0, 60, 60);
		setOpaque(true);
		setLayout(null);
		JLabel stateName = new JLabel(stateGraphicName);
		stateName.setBounds(20,15,30,30);
		this.menu=new JPopupMenu();
		this.makeFinal= new JMenuItem ("Make final");
		this.makeFinal.addActionListener(this);
		this.makeFinal.setActionCommand("Makefinal");
		this.makeInitial= new JMenuItem ("Make initial");
		this.makeInitial.addActionListener(this);
		this.makeInitial.setActionCommand("Makeinitial");	
		this.makeNormal= new JMenuItem ("Make normal");
		this.makeNormal.addActionListener(this);
		this.makeNormal.setActionCommand("Makenormal");	
		menu.add(this.makeNormal);
		menu.add(this.makeFinal);
		menu.add(this.makeInitial);
		add(stateName);
		addMouseListener(this); 

			
		addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseDragged(MouseEvent e) {
				if(e.getButton()==3){
					
				}
				else {
					
					int deltaX = e.getXOnScreen() - screenX;
					int deltaY = e.getYOnScreen() - screenY;
					
					setLocation(myX + deltaX, myY + deltaY);
				}
			}

			@Override
			public void mouseMoved(MouseEvent e) {
			}

		});
	}
	
	public void convertToFinal(){
		setBorder(new LineBorder(Color.RED, 3));
	}
	
	public void convertToNormal(){
		setBorder(new LineBorder(Color.BLUE, 3));
	}
	
	public void convertToInitial(){
		setBorder(new LineBorder(Color.GREEN, 3));
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if(event.getActionCommand().equalsIgnoreCase("Makeinitial")){
			this.convertToInitial();
		}
		
		if(event.getActionCommand().equalsIgnoreCase("Makefinal")){
			this.convertToFinal();
		}
		
		if(event.getActionCommand().equalsIgnoreCase("Makenormal")){
			this.convertToNormal();
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if(e.getButton()==3){
			//Click derecho
		}
		else
		{
		screenX = e.getXOnScreen();
		screenY = e.getYOnScreen();
		
		myX = getX();
		myY = getY();
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	       if(e.getButton()==3){
	         System.out.println(e.getButton());
	         menu.show(this,e.getX(),e.getY());
	       }
	       else
	       {
	    	   
	       }
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}


}